using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.PatientDomain;
using ShivamEMR.Model.DataInterfaces.PatientDataInterf;
using ShivamEMR.Model.DataInterfaces.DoctorDataInterf;
using ShivamEMR.Model;
using ShivamEMR.Model.Domain.CRMDomain;
using ShivamEMR.Web.Areas.Patient.ViewModels;
using ShivamEMR.Model.DataInterfaces.CompanyDataInterf;
using ShivamEMR.Model.DataInterfaces.CRMDataInterf;
//using ShivamEMR.Web.Areas.Patient.Models;

namespace ShivamEMR.Web.Areas.Patient.Controllers
{   
    public class AdmissionController : Controller
    {
		private readonly IAdmissionTypeRepository admissiontypeRepository;
		private readonly IPatientRepository patientRepository;
		private readonly IDoctorRepository doctorRepository;
		private readonly IAdmissionStatusRepository admissionstatusRepository;
		private readonly IAdmissionRepository admissionRepository;
        private readonly IEmployeeRepository employeeRepository;
        private readonly IAssignmentStatusRepository assignmentStatusRepository;
        private readonly IAssignmentRepository assigmentRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public AdmissionController() 
        {
            admissiontypeRepository = Registry.RepositoryFactory.GetAdmissionTypeRepository();
            patientRepository = Registry.RepositoryFactory.GetPatientRepository();
            doctorRepository = Registry.RepositoryFactory.GetDoctorRepository();
            admissionstatusRepository = Registry.RepositoryFactory.GetAdmissionStatusRepository();
            admissionRepository = Registry.RepositoryFactory.GetAdmissionRepository();
            employeeRepository = Registry.RepositoryFactory.GetEmployeeRepository();
            assignmentStatusRepository = Registry.RepositoryFactory.GetAssignmentStatusRepository();
            assigmentRepository = Registry.RepositoryFactory.GetAssignmentRepository();
        }

        public AdmissionController(IAdmissionTypeRepository admissiontypeRepository, IPatientRepository patientRepository, IDoctorRepository doctorRepository, IAdmissionStatusRepository admissionstatusRepository, IAdmissionRepository admissionRepository,IEmployeeRepository emprepo, IAssignmentStatusRepository assignmentStatusRepository, IAssignmentRepository assignmentrepo)
        {
			this.admissiontypeRepository = admissiontypeRepository;
			this.patientRepository = patientRepository;
			this.doctorRepository = doctorRepository;
			this.admissionstatusRepository = admissionstatusRepository;
			this.admissionRepository = admissionRepository;
            this.employeeRepository = emprepo;
            this.assignmentStatusRepository = assignmentStatusRepository;
            this.assigmentRepository = assignmentrepo;
        }

        //
        // GET: /Admission/

        public ViewResult Index()
        {
          //  return View(admissionRepository.AllIncluding(admission => admission.AdmissionType, admission => admission.Patient, admission => admission.ReferredBy, admission => admission.Insurances, admission => admission.Representatives, admission => admission.Assignments, admission => admission.Informations, admission => admission.Diagnosises, admission => admission.Tests, admission => admission.Services, admission => admission.Radiologies));
            return View(admissionRepository.All);
        }


        //
        // GET: /Admission/Details/5

        public ViewResult Details(System.Guid id)
        {
            return View(admissionRepository.Find(id));
        }

        /// <summary>
        /// Create Admission Form for the selected patient
        /// </summary>
        /// <returns></returns>

        public ActionResult New(System.Guid id)
        {
            ViewBag.Patient = patientRepository.Find(id);
            ViewBag.PossibleAdmissionTypes = admissiontypeRepository.All;
            ViewBag.PossibleReferredBies = doctorRepository.All;
            ViewBag.PossibleEmployees = doctorRepository.All;
            var admission = new ShivamEMR.Model.Domain.PatientDomain.Admission() { AdmissionDateAndTime=DateTime.Now };
            return View(admission);
        }


        public string GetEmployeesToAssign(int id)
        {
            string str = string.Empty;
            str = "<select id='emp' name='emp' multiple='multiple'>";
            if (id == 2)
            {
                var emps=employeeRepository.All.Where(e => e.Group.Group == "Nurses" || e.Group.Group == "Doctors").ToList();
                
                foreach (var item in emps)
                {
                    str += "<option Name='" + "emp_" + item.ID + "' value='" + item.ID + "' >" + item.FullName + "</option>";
                }
              
                //ViewBag.PossibleEmployees = emps;
            }
            else
            {
                ViewBag.PossibleEmployees = doctorRepository.All;
                foreach (var item in ViewBag.PossibleEmployees)
                {
                    str += "<option value='" + item.ID + "' >" + item.FullName + "</option>";
                }
            }

            str += "</select>";
            return str;
        }


        public string GetAssignedEmployees(Guid id)
        {
           string str = string.Empty;
            str = "<select id='emp' name='emp' multiple='multiple'>";
               foreach (var item in admissionRepository.Find(id).Assignments)
                {
                    str += "<option Name='" + "emp_" + item.Employee.ID + "' value='" + item.Employee.ID + "' selected='selected' >" + item.Employee.FullName + "</option>";
                }        

            str += "</select>";
            return str;
        }

        [HttpPost]
        public ActionResult New(System.Guid id,AdmissionViewModel avm)
        {

            var emps = Request.Form["emp"].ToString().Split(',');     //.AllKeys.Where(k => k.StartsWith("emp")).ToList();

            var admission = avm.Admission;
            

            if (ModelState.IsValid)
            {
                admission.ID = Guid.NewGuid();
                admission.PatientID = id;
                admission.DischargeDateAndTime = DateTime.Now;
                admission.AdmissionStatusID = 1;// admissionstatusRepository.All.Where(a => a.Status == "InQueue").FirstOrDefault();
                admission.AdmissionDateAndTime = DateTime.Now;
                admission.Assignments = new List<Assignment>();               
                admissionRepository.Create(admission);

                admission.Assignments = new List<Assignment>();
                foreach (var e in emps)
                    admission.Assignments.Add(new Assignment() { ID = Guid.NewGuid(), EmployeeID = Guid.Parse(e), DateAndTime = DateTime.Now, AssignmentStatusID = 1, AssignmentGroupID = 1, Occasion = "Admission", PersonID = id, VenueID = Guid.Parse("FEF12131-3A04-46A6-A3BF-A799F2B6056E") });
                
                admissionRepository.Save();
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.Patient = patientRepository.Find(id);
                ViewBag.PossibleAdmissionTypes = admissiontypeRepository.All;
                ViewBag.PossibleReferredBies = doctorRepository.All;
                ViewBag.PossibleEmployees = doctorRepository.All;
                return View();
            }
        } 

        //
        // GET: /Admission/Create

        public ActionResult Create()
        {
			ViewBag.PossibleAdmissionTypes = admissiontypeRepository.All;
			ViewBag.PossiblePatients = patientRepository.All;
			ViewBag.PossibleReferredBies = doctorRepository.All;
			ViewBag.PossibleAdmissionStatus = admissionstatusRepository.All;
            ViewBag.PossibleEmployees = doctorRepository.All;
            return View();
        } 

        //
        // POST: /Admission/Create

        [HttpPost]
        public ActionResult Create(Admission admission)
        {
            if (ModelState.IsValid) {
                admissionRepository.InsertOrUpdate(admission);
                admissionRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleAdmissionTypes = admissiontypeRepository.All;
				ViewBag.PossiblePatients = patientRepository.All;
				ViewBag.PossibleReferredBies = doctorRepository.All;
				ViewBag.PossibleAdmissionStatus = admissionstatusRepository.All;
                ViewBag.PossibleEmployees = doctorRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Admission/Edit/5
 
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleAdmissionTypes = admissiontypeRepository.All;
			ViewBag.PossiblePatients = patientRepository.All;
			ViewBag.PossibleReferredBies = doctorRepository.All;
			ViewBag.PossibleAdmissionStatus = admissionstatusRepository.All;
            ViewBag.PossibleEmployees = doctorRepository.All;
            ViewBag.AssignedTo = GetAssignedEmployees(id);
             return View(admissionRepository.Find(id));
        }

        //
        // POST: /Admission/Edit/5

        [HttpPost]
        public ActionResult Edit(Admission admission)
        {
            if (ModelState.IsValid) {
                admissionRepository.InsertOrUpdate(admission);
                admissionRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleAdmissionTypes = admissiontypeRepository.All;
				ViewBag.PossiblePatients = patientRepository.All;
				ViewBag.PossibleReferredBies = doctorRepository.All;
				ViewBag.PossibleAdmissionStatus = admissionstatusRepository.All;
                ViewBag.PossibleEmployees = doctorRepository.All;
                ViewBag.AssignedTo = GetAssignedEmployees(admission.ID);
				return View();
			}
        }

        //
        // GET: /Admission/Delete/5
 
        public ActionResult Delete(System.Guid id)
        {
            return View(admissionRepository.Find(id));
        }

        //
        // POST: /Admission/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            var adm = admissionRepository.Find(id);
            admissionRepository.Delete(adm);
            admissionRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

